<?php
/**
 * 测试真实报文解析
 */

// 定义应用根目录
define('APP_PATH', __DIR__ . '/application/');
define('RUNTIME_PATH', __DIR__ . '/runtime/');
define('THINK_PATH', __DIR__ . '/thinkphp/');

// 加载自动加载器
if (!class_exists('Composer\Autoload\ClassLoader')) {
    require_once __DIR__ . '/vendor/autoload.php';
}

use app\service\MessageParser\Vendor2MessageParser;

echo "========================================\n";
echo "真实报文解析测试\n";
echo "========================================\n\n";

$parser = new Vendor2MessageParser();

// 真实的MQTT主题
$topic = 'rziot/zftx_w06/869778076234039/result';

// 真实的消息数据
$messageData = [
    'c' => 'zidianmao',
    'imei' => '869778076234039',
    'iccid' => '89860324462007170717',
    'ssid' => '4G-MIFI-152F',
    'password' => '1234567890',
    'opname' => '46003',
    'dbm' => '-18446744073709551532', // 异常的负数值
    'simSwitch' => '1',
    'ip' => '192.168.100.1',
    'lac' => '30555',
    'version' => 'JZ12SC_ZIDIANMAO_20250730_V1.0.0',
    'cell_id' => '68264372',
    'speed_downlink' => 0,
    'speed_uplink' => 0,
    'sn' => '86977807623403',
    'boot_time' => '131151',
    'count' => '8',
    'hidden' => '0'
];

echo "📡 MQTT主题: {$topic}\n";
echo "📊 消息数据:\n";
echo json_encode($messageData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n\n";

// 验证能处理该消息
echo "🔍 检查是否能处理该消息...\n";
if ($parser->canHandle($topic, $messageData)) {
    echo "✅ 可以处理该消息\n\n";
} else {
    echo "❌ 无法处理该消息\n";
    exit(1);
}

// 解析消息
echo "⚙️  开始解析消息...\n";
$result = $parser->parse($topic, $messageData);

if ($result === null) {
    echo "❌ 解析失败\n";
    exit(1);
}

echo "✅ 解析成功\n\n";

// 显示解析结果
echo "📋 解析结果:\n";
echo "========================================\n";
echo "IMEI: " . $result['IMEI'] . "\n";
echo "MAC: " . $result['mac'] . "\n";
echo "ICCID: " . $result['iccid'] . "\n";
echo "WiFi SSID: " . $result['ssid'] . "\n";
echo "WiFi密码: " . $result['key'] . "\n";
echo "运营商: " . $result['mnc'] . "\n";
echo "信号强度: " . $result['signal_strength'] . " (1-5)\n";
echo "dBm值: " . $result['dbm'] . "\n";
echo "IP地址: " . $result['ip'] . "\n";
echo "LAC: " . $result['lac'] . "\n";
echo "Cell ID: " . $result['cell_id'] . "\n";
echo "固件版本: " . $result['version'] . "\n";
echo "连接数: " . $result['conn_cnt'] . "\n";
echo "更新时间: " . $result['update_time'] . "\n";
echo "========================================\n\n";

// 验证关键字段
echo "🔐 验证关键字段:\n";
$checks = [
    'IMEI' => ['expected' => '869778076234039', 'actual' => $result['IMEI']],
    'iccid' => ['expected' => '89860324462007170717', 'actual' => $result['iccid']],
    'ssid' => ['expected' => '4G-MIFI-152F', 'actual' => $result['ssid']],
    'mnc' => ['expected' => '联通', 'actual' => $result['mnc']],
    'signal_strength' => ['expected' => 3, 'actual' => $result['signal_strength']],
];

$allPassed = true;
foreach ($checks as $field => $check) {
    if ($check['expected'] == $check['actual']) {
        echo "✅ {$field}: {$check['actual']}\n";
    } else {
        echo "❌ {$field}: 期望 {$check['expected']}, 实际 {$check['actual']}\n";
        $allPassed = false;
    }
}

echo "\n";
if ($allPassed) {
    echo "🎉 所有验证通过！真实报文解析成功！\n";
} else {
    echo "⚠️  部分验证失败\n";
    exit(1);
}

